package operation

import (
	"../common"
	"../web"
	"encoding/json"
	"strings"
)

type OperRepairObj struct {
	common.BaseHandler
}

type OperRprActObj struct {
	common.BaseHandler
}

func (self *OperRepairObj) Prepare() {
	codes := *common.FetchAllRowsPtr(
		`SELECT DISTINCT CHNCODE 
		   FROM SFC_REPAIRCODE(NOLOCK) 
		  ORDER BY CHNCODE`,
		self.Usr.Conn, new(common.RecDataStr))
	if self.Usr.Language == "en_US" {
		codes = *common.FetchAllRowsPtr(
			`SELECT DISTINCT ENGCODE 
		       FROM SFC_REPAIRCODE(NOLOCK) 
		      ORDER BY ENGCODE`,
			self.Usr.Conn, new(common.RecDataStr))
	}

	self.TmplData["rprcodes"] = codes
}

func (self *OperRepairObj) ParameterCheck() bool {
	c_ssns := strings.TrimSpace(self.Ctx.Params["ssninputid"])
	c_type := self.Ctx.Params["repairtype"]
	c_ract := strings.TrimSpace(self.Ctx.Params["repairact"])
	c_desc := strings.TrimSpace(self.Ctx.Params["repairdesc"])
	c_flpt := strings.TrimSpace(self.Ctx.Params["failpart"])
	c_oldp := strings.TrimSpace(self.Ctx.Params["oldpart"])
	c_newp := strings.TrimSpace(self.Ctx.Params["newpart"])
	if c_ssns == "" {
		self.TmplData["error"] = common.GetText(self.Lang, "Please input the fail product.")
		return false
	}
	if c_type != "REPLACE" {
		if c_oldp != "" || c_newp != "" {
			self.TmplData["error"] = common.GetText(self.Lang, "Type is not REPLACE, the old/new part can't input.")
			return false
		}
	}
	if c_type == "REPLACE" {
		if c_oldp == "" || c_newp == "" {
			self.TmplData["error"] = common.GetText(self.Lang, "Type is REPLACE, input the old/new part No.")
			return false
		}
	}

	self.Sqlparas = append(self.Sqlparas, c_ssns)
	self.Sqlparas = append(self.Sqlparas, c_type)
	self.Sqlparas = append(self.Sqlparas, c_ract)
	self.Sqlparas = append(self.Sqlparas, c_desc)
	self.Sqlparas = append(self.Sqlparas, c_flpt)
	self.Sqlparas = append(self.Sqlparas, c_oldp)
	self.Sqlparas = append(self.Sqlparas, c_newp)
	self.Sqlparas = append(self.Sqlparas, self.Usr.Username)
	self.Flashparas = []interface{}{strings.ToUpper(c_ssns)}

	return true
}

func (self *OperRepairObj) SqlExecute() bool {
	spRtnData := *common.FetchOneRowPtr(
		`sp_RepairDetail ?, ?, ?, ?, ?, ?, ?, ?`,
		self.Usr.Conn, new(common.SPReturn), self.Sqlparas...)
	if spRtnData.(common.SPReturn).Flag > 0 {
		self.TmplData["error"] = common.GetTextSP(self.Lang, spRtnData)
		return false
	}
	return true
}

func RepairFlowHandler(ctx *web.Context) {
	self := new(OperRepairObj)
	self.Ctx = ctx
	self.Url = "/repairflow"
	self.Html = "repairflow.html"
	self.Rightlist = []string{"repairflow"}
	self.Flashstr = "Repair product: [ %v ] finished."
	self.Do(self)
}

func (self *OperRprActObj) ParameterCheck() bool {
	c_ssns := strings.TrimSpace(self.Ctx.Params["ssn"])
	//if c_ssns == "" {
	//	return false
	//}

	self.Sqlparas = append(self.Sqlparas, c_ssns)

	return true
}

func (self *OperRprActObj) SqlQueryExecute() {
	sRtnData := *common.FetchOneRowPtr(
		`SELECT TOP 1 SSN_NO, ISNULL(FAILCODE,''), ISNULL(FAILDESC,''), ISNULL(FAILLOCA,'') 
           FROM SFC_REPAIR(NOLOCK) 
          WHERE SSN_NO=?
            AND ISREPAIRED=0`,
		self.Usr.Conn, new(common.SsnFailure), self.Sqlparas...)
	code := sRtnData.(common.SsnFailure).Failcode
	desc := sRtnData.(common.SsnFailure).Faildesc
	loca := sRtnData.(common.SsnFailure).Failloca

	var rpract []interface{}
	if self.Lang == "en_US" {
		rpract = *common.FetchAllRowsPtr(
			`SELECT ISNULL(D.ACTION_ENG, ''), ISNULL(D.CYCLE_STATION, '')
		   	   FROM SFC_ROUTING A(NOLOCK),SFC_WO B(NOLOCK),SFC_SSN C(NOLOCK),SFC_REPAIRSETTING D(NOLOCK)
		  	  WHERE C.SSN_NO=? 
		    	AND C.WONO=B.WONO 
		    	AND B.ROUTING=A.NAME 
		    	AND C.CURR_STATION=A.CURR_STATION 
		    	AND A.REPAIR_STATION=D.PROCESS
		      ORDER BY D.IDX`,
			self.Usr.Conn, new(common.RepairAction), self.Sqlparas...)
	} else {
		rpract = *common.FetchAllRowsPtr(
			`SELECT ISNULL(D.ACTION_CHN, ''), ISNULL(D.CYCLE_STATION, '')
			   FROM SFC_ROUTING A(NOLOCK),SFC_WO B(NOLOCK),SFC_SSN C(NOLOCK),SFC_REPAIRSETTING D(NOLOCK)
			  WHERE C.SSN_NO=? 
			    AND C.WONO=B.WONO 
			    AND B.ROUTING=A.NAME 
			    AND C.CURR_STATION=A.CURR_STATION 
			    AND A.REPAIR_STATION=D.PROCESS
			  ORDER BY D.IDX`,
			self.Usr.Conn, new(common.RepairAction), self.Sqlparas...)
	}

	dict_js, _ := json.Marshal(map[string]interface{}{"failcode": code, "faildesc": desc, "failloca": loca, "rpract": rpract, "lang": self.Lang})
	self.Ctx.ResponseWriter.Write(dict_js)
}

func SelectRprSSNHandler(ctx *web.Context) {
	self := new(OperRprActObj)
	self.Ctx = ctx
	self.DoJson(self)
}
